package com.wantupai.translate.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;

public class SspGridlineView extends View {

    /**
     * 竖向线条数,可通过布局属性设置列数
     */
    private final static int linesX = 2;
    /**
     * 竖向线条数，可通过布局属性设置行数
     */
    private final static int linesY = 2;

    private int width;
    private int height;
    private Paint mPaint = null;

    public SspGridlineView(Context context) {
        super(context);
        init();
    }

    public SspGridlineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SspGridlineView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(1);
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        if (wm != null) {
            width = wm.getDefaultDisplay().getWidth();
            height = wm.getDefaultDisplay().getHeight() / 10 * 6;
            setMeasuredDimension(width, height);
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int x = width / (linesX + 1);
        int y = height / (linesY + 1);
        for (int i = 1; i <= linesX; i++) {
            canvas.drawLine(x * i, 0, x * i, height, mPaint);
        }

        for (int i = 1; i <= linesY; i++) {
            canvas.drawLine(0, y * i, width, y * i, mPaint);
        }
    }


}
